package com.boot.unity.api.wx.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.boot.unity.api.wx.service.CommonUtilUrlService;
import com.boot.unity.api.wx.service.ZsWxImageService;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.nio.file.Files;
import java.nio.file.Paths;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class CreateWxCodeFileTask {
    @Value("${serv.apiUrl}")
    private String apiUrl;

    @Autowired
    private CommonUtilUrlService commonUtilUrlService;

    @Autowired
    private ZsWxImageService zsWxImageService;

    @Value("${img.filePath}")
    private String filePath;

    @Value("${serv.qzip}")
    private String qzip;

    @Value("${img.key}")
    private String key;

//    @Scheduled(cron = "0 58 23 * * ?")
//    @Scheduled(fixedRate=30*60*1000)
    public void createWxFile() throws Exception{
        System.out.println("------------------------------进行执行生成招生二维码----------------------------------");
        String url = apiUrl+"/wxcodeinfo/getTodayManager";
        String zsurl = apiUrl+"/wxcodeinfo/backChangeZsUrl";
        //将文件推送到对方的服务器的指定路径下面
        String qzHbFileUrl = "http://"+qzip+":9002/wx/storage/create_jpg_images";
        String data = commonUtilUrlService.post(url, ""); // 获取需要生成招生码的数据
        JSONArray jsonArray = JSONArray.parseArray(data);
        // 进行遍历数据
        if(jsonArray.size() > 0){
            for(Object obj:jsonArray){
                /**
                 * {"qrurl":"https://tp.jpk.weihai.cn/images/244370620196806031033.png",
                 * "school_id":3341,"bindwxurl":"","data_id":244,"idcard":"370620196806031033",
                 * "name":"刘文广","school_name":"威海交运驾驶员培训中心有限公司",
                 * "id":"244370620196806031033",
                 * "zsurl":"https://tp.jpk.weihai.cn/images/244370620196806031033hb.jpg",
                 * "add_time":1720749826000}
                 */
                JSONObject json = (JSONObject) obj;
                String id = json.getString("id");

                // System.out.println(json.toString());
                // 生成小程序招生PNG文件
                zsWxImageService.createWxQrCodeFile(id);
                // 生成小程序的招生海报
                zsWxImageService.createJPGManagerHb(json);

                //进行回调，更新请求地址
                JSONObject resultJson = new JSONObject();
                resultJson.put("idkey",id);
                commonUtilUrlService.post(zsurl, resultJson.toString()); // 获取需要生成招生码的数据

                //将文件推送到17服务器的图片文件夹中
                String managerId = json.getString("id");
                String jpgPath = filePath+"images//" + managerId + "hb.jpg";

                byte[] fileContent = Files.readAllBytes(Paths.get(jpgPath));
                String base64String = Base64.encodeBase64String(fileContent);


                JSONObject resultJpgJson = new JSONObject();
                resultJpgJson.put("key",key);
                resultJpgJson.put("imgStr",base64String);
                resultJpgJson.put("photoName", managerId + "hb.jpg");

                commonUtilUrlService.post(qzHbFileUrl, resultJpgJson.toString()); // 获取需要生成招生码的数据

            }
        }
    }
}
